package com.yuven.appframework.core.thread;

import android.os.Handler;
import android.os.Looper;
import android.os.Process;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class ThreadManager {
    public static final String BackgroundThread = "rd_Background_HandlerThread";
    public static final String RealTimeThread = "rd_RealTime_HandlerThread";
    public static final String NormalThread = "rd_normal_HandlerThread";
    private static Map<String,BaseHandleThread> mHandlerThread = new HashMap<String,BaseHandleThread>();
    private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

    public static ThreadPoolExecutor mThreadPoolExecutor;
    public static Object mThreadPoolLock = new Object();

    public synchronized static BaseHandleThread getHandlerThread(String ThreadName)
    {
        BaseHandleThread thread = mHandlerThread.get(ThreadName);
        if(null == thread)
        {
            thread = new BaseHandleThread(ThreadName,getThreadPriority(ThreadName));
            thread.start();
            mHandlerThread.put(ThreadName,thread);
        }else{
            if(!thread.isAlive())
                thread.start();
        }

        return thread;
    }


    private static int getThreadPriority(String threadName)
    {
        if(BackgroundThread.equals(threadName))
            return Process.THREAD_PRIORITY_BACKGROUND;
        else if(RealTimeThread.equals(threadName))
            return Process.THREAD_PRIORITY_FOREGROUND;
        else
            return Process.THREAD_PRIORITY_DEFAULT;
    }

    public static Handler getMainThreadHandler() {
        return mainThreadHandler;
    }

    public static void removeUIRunnable(Runnable runnable){
        mainThreadHandler.removeCallbacks(runnable);
    }

    public static void runToBackgroundThread(Runnable r){
        runToBackgroundThread(r,0);
    }

    public static void runToBackgroundThread(Runnable r,long delay){
        ThreadManager.getHandlerThread(BackgroundThread).getHandler().postDelayed(r,delay);
    }

    public static void runToRealtimeThread(Runnable r){
        runToRealtimeThread(r,0);
    }
    public static void runToRealtimeThread(Runnable r,long delay){
        ThreadManager.getHandlerThread(RealTimeThread).getHandler().postDelayed(r,delay);
    }

    public static void removeBackgroundThreadRunnable(Runnable r){
        ThreadManager.getHandlerThread(BackgroundThread).getHandler().removeCallbacks(r);
    }

    public static void runToNormalThread(Runnable r){
        runToNormalThread(r,0);
    }

    public static void runToNormalThread(Runnable r,long delay){
        ThreadManager.getHandlerThread(NormalThread).getHandler().postDelayed(r,delay);
    }

    public static void runTRealTimeThread(Runnable r){
        runTRealTimeThread(r,0);
    }

    public static void runTRealTimeThread(Runnable r,long delay){
        ThreadManager.getHandlerThread(RealTimeThread).getHandler().postDelayed(r,delay);
    }


    public static void runToMainThread(Runnable r){
        runToMainThread(r,0);
    }

    public static void runToMainThread(Runnable r,long delay){
        getMainThreadHandler().postDelayed(r,delay);
    }


    //线程池调度
    public static void runToThreadPool(Runnable r){
        synchronized (mThreadPoolLock){
            if(mThreadPoolExecutor == null){
                mThreadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),68,20, TimeUnit.SECONDS,new LinkedBlockingQueue<>(300));
            }
        }
        mThreadPoolExecutor.execute(r);
    }

    public static void removeRunnable(Runnable r){
        if(mThreadPoolExecutor != null) {
            mThreadPoolExecutor.remove(r);
        }
    }

}
